package gui.jtab.leaf;

import event.changeHoja.ChangeHoja;
import event.changeHoja.EventChangeHoja;
import event.notifyDentacionesHoja.EventNotifyDentations;
import event.notifyDentacionesHoja.NotifyDentations;
import event.notifyDentacionesHoja.NotifyDentationsListenerInterface;
import figuras.PuntoVector;
import gui.GLListeners.GLListener;
import gui.GLListeners.GLListenerCreacionHoja;
import gui.GLListeners.GLListenerCurvarHoja;
import gui.GLListeners.GLListenerVisor;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Observable;
import java.util.Properties;

import javax.swing.JPanel;

import properties.Config;
import utils.files.FileManager;
import utils.files.FileManagerFlowers;

import componentes.flor.Hoja;
import componentes.flor.HojaDentada;

import constantes.Consts;

public class JTabObjectSepal extends JTabObjectLeaf implements NotifyDentationsListenerInterface {
	
	private static final long serialVersionUID = 1L;

	protected JPanel dentationPanel;
		
	public JTabObjectSepal(){
		super(Consts.TEXTURE_SEPALS_FOLDER);
		
		try {
			PuntoVector eye;
			PuntoVector look;
			PuntoVector up;
			Config config;
			config = Config.getInstance();
			String eyeProperties = config.getString("sepalo.visor1.eye");
			String lookProperties = config.getString("sepalo.visor1.look");
			String upProperties = config.getString("sepalo.visor1.up");
			String[] coordEye = eyeProperties.split(" ");
			String[] coordLook = lookProperties.split(" ");
			String[] coordUp = upProperties.split(" ");
			eye = new PuntoVector(Integer.parseInt(coordEye[0]), Integer
					.parseInt(coordEye[1]), Integer.parseInt(coordEye[2]), 1);
			look = new PuntoVector(Integer.parseInt(coordLook[0]), Integer
					.parseInt(coordLook[1]), Integer.parseInt(coordLook[2]), 1);
			up = new PuntoVector(Integer.parseInt(coordUp[0]), Integer
					.parseInt(coordUp[1]), Integer.parseInt(coordUp[2]), 1);
			((GLListenerVisor)GLLVisor1).setEye(eye);
			((GLListenerVisor)GLLVisor1).setLook(look);
			((GLListenerVisor)GLLVisor1).setUp(up);

			eyeProperties = config.getString("sepalo.visor2.eye");
			lookProperties = config.getString("sepalo.visor2.look");
			upProperties = config.getString("sepalo.visor2.up");
			coordEye = eyeProperties.split(" ");
			coordLook = lookProperties.split(" ");
			coordUp = upProperties.split(" ");
			eye = new PuntoVector(Integer.parseInt(coordEye[0]), Integer
					.parseInt(coordEye[1]), Integer.parseInt(coordEye[2]), 1);
			look = new PuntoVector(Integer.parseInt(coordLook[0]), Integer
					.parseInt(coordLook[1]), Integer.parseInt(coordLook[2]), 1);
			up = new PuntoVector(Integer.parseInt(coordUp[0]), Integer
					.parseInt(coordUp[1]), Integer.parseInt(coordUp[2]), 1);

			((GLListenerVisor)GLLVisor2).setEye(eye);
			((GLListenerVisor)GLLVisor2).setLook(look);
			((GLListenerVisor)GLLVisor2).setUp(up);
			
			eyeProperties = config.getString("sepalo.visor3.eye");
			lookProperties = config.getString("sepalo.visor3.look");
			upProperties = config.getString("sepalo.visor3.up");
			coordEye = eyeProperties.split(" ");
			coordLook = lookProperties.split(" ");
			coordUp = upProperties.split(" ");
			eye = new PuntoVector(Integer.parseInt(coordEye[0]), Integer
					.parseInt(coordEye[1]), Integer.parseInt(coordEye[2]), 1);
			look = new PuntoVector(Integer.parseInt(coordLook[0]), Integer
					.parseInt(coordLook[1]), Integer.parseInt(coordLook[2]), 1);
			up = new PuntoVector(Integer.parseInt(coordUp[0]), Integer
					.parseInt(coordUp[1]), Integer.parseInt(coordUp[2]), 1);
			eye = new PuntoVector(300, 300, 300, 1);
			look = new PuntoVector(0, 0, 0, 1);
			up = new PuntoVector(0, 1, 0, 0);
			((GLListenerVisor)GLLVisor3).setEye(eye);
			((GLListenerVisor)GLLVisor3).setLook(look);
			((GLListenerVisor)GLLVisor3).setUp(up);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setName("sepal");
		dentationPanel = new JTabDentationOptions();
		tabOptionsPanel.addTab("Dentaciones",dentationPanel);
		NotifyDentations notifyDenationsManager = new NotifyDentations();
		notifyDenationsManager.addEventNotifyDentationsListener(this);
		((JTabDentationOptions) dentationPanel).addNotifyDentations(notifyDenationsManager);
	}
		
	public void nuevo() {
		enabledMenuGuardar = false;
		FileManagerFlowers.setSepal(null);
		canvas.getContext().makeCurrent();
		canvas.removeGLEventListener(GLL);
		GLL = new GLListenerCreacionHoja();
		ChangeHoja changeHojaManager = new ChangeHoja();
		changeHojaManager.addEventChangeHojaListener(this);
		((GLListenerCreacionHoja) GLL).addChangeHoja(changeHojaManager);
		GLL.init(canvas);
		canvas.setGllistener((GLListener) GLL);
		canvas.addGLEventListener(GLL);
		canvas.repaint();
		canvas.requestFocus();

		GLLVisor1.setComponente(null);
		GLLVisor2.setComponente(null);
		GLLVisor3.setComponente(null);
		visor1.repaint();
		visor2.repaint();
		visor3.repaint();		
	}
	
	@Override
	public void abrir(File file) {
		String sepal = file.getAbsolutePath();
		sepal = FileManagerFlowers.setSepal(sepal);	
		sepal = sepal+file.getName().substring(0,file.getName().indexOf("."));

		canvas.getContext().makeCurrent();
		canvas.removeGLEventListener(GLL);
		GLL = new GLListenerCurvarHoja();

		GLL.addObserver(visor1);
		GLL.addObserver(visor2);
		GLL.addObserver(visor3);
		GLL.addObserver(this);
		Hoja hoja = new Hoja(canvas.getGL());
		((GLListenerCurvarHoja) GLL).setHoja(hoja);
		((GLListenerCurvarHoja) GLL).cargar(sepal);
		((GLListenerCurvarHoja) GLL).trasladar();
		
		GLL.init(canvas);
		canvas.setGllistener((GLListener) GLL);
		canvas.addGLEventListener(GLL);
		cargarTextura(sepal);
		canvas.repaint();
		canvas.requestFocus();

		GLLVisor1.setComponente(GLL.getComponente());
		GLLVisor2.setComponente(GLL.getComponente());
		GLLVisor3.setComponente(GLL.getComponente());
		visor1.repaint();
		visor2.repaint();
		visor3.repaint();	
		enabledMenuGuardar = true;
	}

	@Override
	public void guardar() {
		String sepal = FileManagerFlowers.getSepal();
		guardarComo(new File(sepal.substring(0, sepal.lastIndexOf("."))));
		makeCurrentContext();
	}

	@Override
	public void update(Observable observable, Object arg) {
		if(arg!=null &&arg.equals("deformaHoja")){
			((JTabDentationOptions) dentationPanel).newDentations();
		}
		visor1.repaint();
		visor2.repaint();
		visor3.repaint();		
	}
	
	public void catchChangeHojaListener(EventChangeHoja eventChangeHoja) {
		canvas.getContext().makeCurrent();
		canvas.removeGLEventListener(GLL);
		GLL = new GLListenerCurvarHoja();

		GLL.addObserver(visor1);
		GLL.addObserver(visor2);
		GLL.addObserver(visor3);
		GLL.addObserver(this);
		Hoja hoja = (Hoja) eventChangeHoja.getSource();
		hoja.setGl(canvas.getGL());
		((GLListenerCurvarHoja) GLL).setHoja(hoja);
		GLL.init(canvas);
		canvas.setGllistener((GLListener) GLL);
		canvas.addGLEventListener(GLL);
		canvas.repaint();
		canvas.requestFocus();

		GLLVisor1.setComponente(GLL.getComponente());
		GLLVisor2.setComponente(GLL.getComponente());
		GLLVisor3.setComponente(GLL.getComponente());
		((JTabDentationOptions) dentationPanel).setHojaDentada((HojaDentada) GLL
				.getComponente());
		visor1.repaint();
		visor2.repaint();
		visor3.repaint();
		((JTabDentationOptions) dentationPanel).newDentations();	
	}

	@Override
	public void catchNotifyDentationsListener(EventNotifyDentations evt) {
		makeCurrentContext();		
	}
	
	@Override
	public void almacenaEnGaleria(File file) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String name = Consts.SAVE_SEPALS_FOLDER+format.format(new Date());
		File directory = new File(name);
		guardarComo(directory);
		directory.mkdir();
		FileManager.unZipComponent(name+".flw", name+"/");
		makeCurrentContext();
	}	
	
	protected void cargarTextura(String path){
		super.cargarTextura(path);
		File directory = new File(path);
		directory = directory.getParentFile();
		File[] files = directory.listFiles();
		for(File f: files){
			if(f.getName().endsWith(".properties")){
				try {
					Properties properties = new Properties();
					InputStreamReader input = new InputStreamReader(new FileInputStream(f));
					properties.load(input);
					boolean denticion = new Boolean(properties.getProperty("dentition"));
					int nDent = new Integer(properties.getProperty("dentition.number"));
					int max = new Integer(properties.getProperty("dentition.height.max"));
					int min = new Integer(properties.getProperty("dentition.height.min"));
					int alg = new Integer(properties.getProperty("dentition.alg"));
					int red = 0;
					int green = 255;
					int blue = 0;
					if (properties.getProperty("color.r")!=null){
						red = new Integer(properties.getProperty("color.r"));
					}
					if (properties.getProperty("color.g")!=null){
						green = new Integer(properties.getProperty("color.g"));
					}
					if (properties.getProperty("color.b")!=null){
						blue = new Integer(properties.getProperty("color.b"));
					}
					((JTabDentationOptions)dentationPanel).setDentaciones(denticion,nDent,max,min,alg);
					((JTabDentationOptions)dentationPanel).setHojaDentada((HojaDentada)GLL.getComponente());
					this.setColor(new Color(red,green,blue));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}	
		}
	}

}
